﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using Arthas.Controls.Metro;
using Arthas.Utility.Media;
using System.Reflection;
using System.Windows.Threading;
using System.Runtime.InteropServices;
using System.Configuration;

namespace ELM
{
    /// <summary>
    /// Login.xaml 的交互逻辑
    /// </summary>
    public partial class Login : MetroWindow
    {
        public Login()
        {
            InitializeComponent();
            //如果有多人则取出最后一对
            if (ConfigurationManager.AppSettings.Count != 0)
            {
                foreach (string key in ConfigurationManager.AppSettings)
                {
                    //int i = 1;
                    if(ConfigurationManager.AppSettings[key] == ConfigurationManager.AppSettings[ConfigurationManager.AppSettings.Count - 1])
                    {
                        EmployeeId.Text = key;
                        Password.Text = ConfigAccess.GetAppConfig(key);
                    }
                    //if (i == ConfigurationManager.AppSettings.Count)
                    //{

                    //}
                    //i++;
                   
                }
            }
        }

        private void MetroButton_Click_Reset(object sender, RoutedEventArgs e)
        {
            EmployeeId.Text = "";Password.Text = "";
        }

        private void MetroButton_Click_Login(object sender, RoutedEventArgs e)
        {
            string sEmployeeId = EmployeeId.Text.Trim();
            string sPassword = Password.Text.Trim();
            //自己输入的情况 加密
            string sqlcommand = String.Format("select count(*) from Account where Employee_Id='{0}'and Pass_Word='{1}' ", sEmployeeId, Md5.CreateMd5(sPassword));
            //给加密过的config用
            string sqlcommand3 = String.Format("select count(*) from Account where Employee_Id='{0}'and Pass_Word='{1}' ", sEmployeeId, sPassword);
            string sqlcommand1 = String.Format("select count(*) from  Employee where Employee_Id='{0}'", sEmployeeId);
            //员工表里存在（在职）
            if (Convert.ToInt32(DataAccess.ExecuteScalar(sqlcommand1)) > 0)
            {
                //账号表里账号密码对正确
                if (Convert.ToInt32(DataAccess.ExecuteScalar(sqlcommand)) > 0 || Convert.ToInt32(DataAccess.ExecuteScalar(sqlcommand3)) > 0)
                {
                    //登录成功后查找数据库找出职位和姓名并用Brige保存 以便之后的权限限
                    string sqlcommand2 = String.Format("select Name,Position from Employee where Employee_Id='{0}'", sEmployeeId);
                    System.Data.DataTable s = DataAccess.GetResult(sqlcommand2); ;
                    Bridge.Name = s.Rows[0][0].ToString();
                    Bridge.Position = s.Rows[0][1].ToString();
                    Bridge.ID = sEmployeeId;

                    //如果打开记住密码则吧账号,加密后密码写入config
                    if (IsKeepPwd.IsChecked == true)
                    {
                        ConfigAccess.UpdateAppConfig(sEmployeeId, Md5.CreateMd5(sPassword));
                    }
                    MainWindow win = new MainWindow();
                    win.Title = " 欢迎你," + Bridge.Name;
                    this.Close();
                    win.Show();
                }
                //密码表无记录但员工表有时允许用默认密码
                else
                {
                    if (sPassword.Equals("Aa123456."))
                    {
                        //要求修改密码修改密码并重新登陆
                        ChangePwd chan = new ChangePwd();
                        chan.EmployeeId.Text = sEmployeeId;
                        chan.EmployeeId.IsEnabled = false;
                        chan.Show();
                        this.Close();
                    }
                    else {
                        MessageBox.Show("密码输入不正确");
                    }
                }

            }
            else
            {
                MessageBox.Show("您输入的账号不存");
            }
        }

        private void MetroSwitch_Click_IsKeepPwd(object sender, RoutedEventArgs e)
        {
            if (IsKeepPwd.IsChecked ==true)
            {
                IsKeepPwd.Content = "记住密码打开                ";
            }
            else
            {
                IsKeepPwd.Content = "记住密码关闭                ";
            }
            // MessageBox.Show(Md5.CreateMd5("Aa123456."));4e7102969fcd46813b869e931aedf4b
        }

        //密码框大小写提示
        //导入方法
        [DllImport("user32.dll", EntryPoint = "GetKeyboardState")]
        public static extern int GetKeyboardState(byte[] pbKeyState);
        //使用方法
        public static bool CapsLockStatus
        {
            get
            {
                byte[] bs = new byte[256];
                GetKeyboardState(bs);
                return (bs[0x14] == 1);
            }
        }
        private void Password_KeyDown(object sender, KeyEventArgs e)
        {
            if (CapsLockStatus == true)
            {
                Password.PopupHint = "您输入为大写字母或数字";
            }
            else
            {
                Password.PopupHint = "您输入为小写字母或数字";
            }     
        }
    }
}
